package com.cee.service.Impl;

import com.cee.common.result.ResultCodeEnum;
import com.cee.dao.ExamUserMapper;
import com.cee.dto.*;
import com.cee.exception.CeeException;
import com.cee.model.*;
import com.cee.service.*;
import com.cee.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ExamUserServiceImpl implements ExamUserService {

    @Autowired
    private ExamUserMapper examUserMapper;

    @Autowired
    private OptionOneSelectService optionOneSelectService;
    @Autowired
    private OptionManySelectService optionManySelectService;
    @Autowired
    private OptionJudgeService optionJudgeService;



    @Autowired
    private ExamService examService;


    @Autowired
    private ExamUserOneService examUserOneService;
    @Autowired
    private ExamUserManyService examUserManyService;
    @Autowired
    private ExamUserJudgeService examUserJudgeService;
    @Autowired
    private ExamUserCompletionService examUserCompletionService;
    @Autowired
    private ExamUserShortService examUserShortService;


    @Autowired
    private ExamOneSelectService examOneSelectService;
    @Autowired
    private ExamManySelectService examManySelectService;
    @Autowired
    private ExamJudgeService examJudgeService;
    @Autowired
    private ExamCompletionService examCompletionService;
    @Autowired
    private ExamShortAnswerService examShortAnswerService;

    @Override
    public List<ExamUserDTO> getExamUserList(ExamUserDTO examUserDTO) {
        return examUserMapper.getExamUserList(examUserDTO);
    }

    @Override
    public void saveExamUser(ExamUser examUser) {


        if (examUser.getCeeExamId() == 0 || examUser.getExamEndDate() == null || examUser.getExamStartDate() == null
                || examUser.getTimeLimit() == 0 || StringUtils.isEmpty(examUser.getUserName())
                || StringUtils.isEmpty(examUser.getUserPhone()))
        {
            throw new CeeException(ResultCodeEnum.PARAM_ERROR);
        }

        // 验证手机号是否存在，  一人面试多个岗位。
//        ExamUser eu = examUserMapper.getExamUserByPhone(examUser.getUserPhone());
//        if (eu != null){ // 存在则不让提交
//            throw new CeeException(ResultCodeEnum.ERROR_901);
//        }

        log.info("考试添加试卷, examUser={}", examUser);
        // 添加主表
        examUserMapper.insert(examUser);



        // 以下添加保存 考试提交信息.
//        Integer ceeExamUserId = examUser.getId();// 考试ID
//
//
//        Integer ceeExamId = examUser.getCeeExamId(); // 试卷ID
//
//        List<ExamOneSelect> examOneSelects = examOneSelectService.getListByExamId(ceeExamId);
//        if (examOneSelects != null){
//            ExamUserOne examUserOne = new ExamUserOne();
//            for (ExamOneSelect examOneSelect : examOneSelects){
//                examUserOne.setCeeExamId(ceeExamId);
//                examUserOne.setCeeExamUserId(ceeExamUserId);
//                examUserOne.setCeeOneSelectId(examOneSelect.getCeeOneSelectId());
//
//                // 添加
//                log.info("考试添加试卷时，保存考试-题目关系，examUserOne={}", examUserOne);
//                examUserOneService.saveExamUserOne(examUserOne);
//            }
//        }
//
//
//
//        List<ExamManySelect> examManySelects = examManySelectService.getListByExamId(ceeExamId);
//        if (examManySelects != null){
//            ExamUserMany examUserMany = new ExamUserMany();
//            for (ExamManySelect examManySelect : examManySelects){
//                examUserMany.setCeeExamId(ceeExamId);
//                examUserMany.setCeeExamUserId(ceeExamUserId);
//                examUserMany.setCeeManySelectId(examManySelect.getCeeManySelectId());
//
//                // 添加
//                log.info("考试添加试卷时，保存考试-题目关系，examUserMany={}", examUserMany);
//                examUserManyService.saveExamUserMany(examUserMany);
//            }
//        }
//
//        List<ExamJudge> examJudges = examJudgeService.getListByExamId(ceeExamId);
//        if (examJudges != null){
//            ExamUserJudge examUserJudge = new ExamUserJudge();
//            for (ExamJudge examJudge : examJudges){
//                examUserJudge.setCeeExamId(ceeExamId);
//                examUserJudge.setCeeExamUserId(ceeExamUserId);
//                examUserJudge.setCeeJudgeId(examJudge.getCeeJudgeId());
//
//                // 添加
//                log.info("考试添加试卷时，保存考试-题目关系，examUserJudge={}", examUserJudge);
//                examUserJudgeService.saveExamUserJudge(examUserJudge);
//            }
//        }
//
//
//        List<ExamCompletion> examCompletions = examCompletionService.getListByExamId(ceeExamId);
//        if (examCompletions != null){
//            ExamUserCompletion examUserCompletion = new ExamUserCompletion();
//            for (ExamCompletion examCompletion : examCompletions){
//                examUserCompletion.setCeeExamId(ceeExamId);
//                examUserCompletion.setCeeExamUserId(ceeExamUserId);
//                examUserCompletion.setCeeCompletionId(examCompletion.getCeeCompletionId());
//
//                // 添加
//                log.info("考试添加试卷时，保存考试-题目关系，examUserCompletion={}", examUserCompletion);
//                examUserCompletionService.saveExamUserCompletion(examUserCompletion);
//            }
//        }
//
//
//        List<ExamShortAnswer> examShortAnswers = examShortAnswerService.getListByExamId(ceeExamId);
//        if (examShortAnswers != null){
//            ExamUserShort examUserShort = new ExamUserShort();
//            for (ExamShortAnswer examShortAnswer : examShortAnswers){
//                examUserShort.setCeeExamId(ceeExamId);
//                examUserShort.setCeeExamUserId(ceeExamUserId);
//                examUserShort.setCeeShortAnswerId(examShortAnswer.getCeeShortAnswerId());
//
//                // 添加
//                log.info("考试添加试卷时，保存考试-题目关系，examUserShort={}", examUserShort);
//                examUserShortService.saveExamUserShort(examUserShort);
//            }
//        }


    }

    @Override
    public ExamUserResultDTO getExamUser(Integer id) {

        ExamUserDTO examUserDTO = examUserMapper.getExamUser(id);

        ExamUserResultDTO examUserResultDTO = BeanUtil.copy(examUserDTO, ExamUserResultDTO.class);

        // OneSelect  List  单选+选项

        List<ExamUserOneResultDTO> oneList = examUserMapper.getExamUserOneSelect(examUserResultDTO.getId());
        List<ExamUserOneResultDTO> rsOneSelects = new ArrayList<>();

        if (oneList != null){
            for (ExamUserOneResultDTO one : oneList){
                List<OptionOneSelect> olist = optionOneSelectService.getListById(one.getId());
                one.setList(olist);
                rsOneSelects.add(one);
            }

            examUserResultDTO.setOneSelects(rsOneSelects);
        }

        // Judge  List 判断 + 选项

        List<ExamUserManyResultDTO> manyList = examUserMapper.getExamUserManySelect(examUserResultDTO.getId());
        List<ExamUserManyResultDTO> rsManySelects = new ArrayList<>();

        if (manyList != null){
            for (ExamUserManyResultDTO many : manyList){
                List<OptionManySelect> olist = optionManySelectService.getListById(many.getId());
                many.setList(olist);
                rsManySelects.add(many);
            }

            examUserResultDTO.setManySelects(rsManySelects);
        }

        // Judge  List 判断 + 选项
        List<ExamUserJudgeResultDTO> judgeList = examUserMapper.getExamUserJudge(examUserResultDTO.getId());
        List<ExamUserJudgeResultDTO> rsJudges = new ArrayList<>();

        if (manyList != null){
            for (ExamUserJudgeResultDTO judge : judgeList){
                List<OptionJudge> olist = optionJudgeService.getListById(judge.getId());
                judge.setList(olist);
                rsJudges.add(judge);
            }

            examUserResultDTO.setJudges(rsJudges);
        }


        // Completion  List 填空
        List<ExamUserCompletionResultDTO> completionList = examUserMapper.getExamUserCompletion(examUserResultDTO.getId());
        examUserResultDTO.setCompletions(completionList);


        // ShortAnswer  List 简答
        List<ExamUserShortResultDTO> shortList = examUserMapper.getExamUserShortAnswer(examUserResultDTO.getId());
        examUserResultDTO.setShortAnswers(shortList);

        return examUserResultDTO;
    }

    @Override
    public void deleteExamUserById(Integer id) {

        log.info("物理删除考试ExamUser，id={}", id);
        examUserMapper.deleteById(id);
    }

    @Override
    public void updateExamUser(ExamUserUpdDTO examUserUpdDTO) {
        if (examUserUpdDTO.getCompletions() != null){
            for (ExamUserCompletionResultDTO dto : examUserUpdDTO.getCompletions()){
                ExamUserCompletion examUserCompletion = new ExamUserCompletion();
                examUserCompletion.setId(dto.getCeeExamUserCompletionId());
                examUserCompletion.setScore(BigDecimal.valueOf(dto.getScore()));

                log.info("打分填空题，examUserCompletion={}", examUserCompletion);
                examUserCompletionService.updateExamUserCompletion(examUserCompletion);
            }
        }

        if (examUserUpdDTO.getShortAnswers() != null){
            for (ExamUserShortResultDTO dto : examUserUpdDTO.getShortAnswers()){
                ExamUserShort examUserShort = new ExamUserShort();
                examUserShort.setId(dto.getCeeExamUserShortId());
                examUserShort.setScore(BigDecimal.valueOf(dto.getScore()));

                log.info("打分简答题，examUserShort={}", examUserShort);
                examUserShortService.updateExamUserShort(examUserShort);
            }
        }


        Integer scoreTotal = countScoreTotal(examUserUpdDTO.getCeeExamUserId()); // 总得分

        ExamUser examUser = new ExamUser();
        examUser.setId(examUserUpdDTO.getCeeExamUserId());
        examUser.setStatus("endScore");
        examUser.setScoreTotal(BigDecimal.valueOf(scoreTotal));

        log.info("打分完成后更新总得分和状态，examUser={}", examUser);
        updateStatusAndScoreTotal(examUser);
    }

    @Override
    public void updateStatusAndScoreTotal(ExamUser examUser) {
        examUserMapper.updateExamUser(examUser);
    }

    @Override
    public Integer countScoreTotal(Integer id) {
        return examUserMapper.countScoreTotal(id);
    }

}
